package com.api.gbhg.lsq.action;

import com.alibaba.fastjson.JSONObject;
import com.api.gbhg.lbj.action.SourceAction;
import com.api.gbhg.liwenjun.VersionFlowUtil;
import com.api.gbhg.zengping.common.CommonDao;

import weaver.conn.RecordSet;
import weaver.conn.RecordSetTrans;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.DetailTable;
import weaver.soa.workflow.request.RequestInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.math.BigDecimal;
import java.util.*;

/**
 * @author lsq
 * @version 1.0
 * @description: TODO 同步报销数据到报销数据记录表(明细表)
 * @date 2021/4/20 16:52
 */
public class SynchronizeReimbursementDataAction implements Action {
    private static final Logger logger = LoggerFactory.getLogger("gbhg");
    private final SourceAction sou = new SourceAction();

    //@Override
    public String execute(RequestInfo requestInfo) {

        //请求id
        String requestid = requestInfo.getRequestid();
        logger.info("进入明细报销后同步报销uf_bxsjjl表---"+requestid);
        RecordSet del = new RecordSet();
        del.execute("delete from uf_bxsjjl where lcid="+requestid);
        RecordSetTrans rst = new RecordSetTrans();
        rst.setAutoCommit(false);
        //得到明细表名
        DetailTable[] detailTables = CommonDao.getDetailTables(requestInfo);

        //当前流程表名
        String billTableName = requestInfo.getRequestManager().getBillTableName();
        //根据请求id查询主表信息
        RecordSet rs = CommonDao.selectByRequestId(new RecordSet(), billTableName, requestid);
        rs.next();
        //得到主表id
        String mainid = rs.getString("id");
        //根据表名称主表id 查询明细信息
        RecordSet recordSet = CommonDao.selectByDetailTable(detailTables[0].getTableDBName(), mainid);
        Map<String, String>[] list = new HashMap[recordSet.getArray().size()];
        int i = 0;
        boolean b2 = true;
        String ysxm = "";
        while (recordSet.next()) {
            list[i] = new HashMap<>();

            //流程类型 0:主表同步,1:明细表同步
            String lclx = "1";
            //流程id:requestid
            //明细id
            String mxid = recordSet.getString("id");
            //预算项目id
            ysxm = recordSet.getString("ysxm");
            //申请报销金额
            String sqbxje = recordSet.getString("sqbxje");
            //同意报销金额
            String tybxje = rs.getString("tybxje");
            //部门经济分类
            String bmjjzcfl = recordSet.getString("bmjjzcfl");
            //政府经济分类
            String zfjjzcfl = recordSet.getString("zfjjzcfl");
            //是否已提交  0:未提交,1:已提交 默认为未提交
            String sfytj = "1";
            String cxje = recordSet.getString("cxje");


            list[i].put("lclx", lclx);//流程类型
            list[i].put("lcid", requestid);//流程id
            list[i].put("mxid", mxid);//明细id
            list[i].put("ysxm", ysxm);//预算项目id
            list[i].put("sqbxje", sqbxje);//申请报销金额
            list[i].put("tybxje", tybxje);//同意报销金额
            list[i].put("bmjjfl", bmjjzcfl);//部门经济支出分类
            list[i].put("zfjjfl", zfjjzcfl);//政府经济支出分类
            list[i].put("sfytj", sfytj);//是否已提交
            list[i].put("cxje", cxje);//冲销金额

            ++i;
            logger.info("明细表申请报销金额:" + sqbxje + "同意报销金额:" + tybxje);

        }
        // 存储明细表报销数据到经费来源表
        b2 = storageDateToBaoXiaoSj(list,rst);
        if (!b2) {
            rst.rollback();
            logger.info("============================提交节点后保存报销数据出现错误(明细表)======================================");
            requestInfo.getRequestManager().setMessagecontent("提交节点后保存报销数据出现错误(明细表)");
            requestInfo.getRequestManager().setMessageid("1");
            return FAILURE_AND_CONTINUE;
        }
        rst.commit();
        return Action.SUCCESS;
    }


    public boolean storageDateToBaoXiaoSj(Map[] list,RecordSetTrans rst) {
        boolean flag = false;
        for (int i = 0; i < list.length; i++) {
            //删除key为null的值
            Map map = sou.removeNullString(list[i]);
            Set set = map.keySet();
            List<String> listField = new ArrayList<String>(set);

            // 报销数据
            Collection<String> valueCollection = map.values();
            List<String> valueList = new ArrayList<>(valueCollection);
            // 存储数据到报销数据记录表
            String uf_baoxiaoshujusql = VersionFlowUtil.getInsertSql(listField, "uf_bxsjjl");
            try {
                logger.info("同步报销流程sql---->" + uf_baoxiaoshujusql);
                rst.executeUpdate(uf_baoxiaoshujusql, valueList.toArray());
                logger.info("同步报销流程sql数据---->"+ JSONObject.toJSONString(valueList));
            } catch (Exception e) {
                e.printStackTrace();
                logger.info("同步报销流程sql---->" + uf_baoxiaoshujusql + valueCollection);
                logger.info("errorsql==" + e.toString());
                flag = false;
            }
            flag = true;
        }
        return flag;
    }
}
